//
//  BTProfileViewController.m
//  WeChat
//
//  Created by 互联网 on 16/11/2.
//  Copyright © 2016年 bigtree. All rights reserved.
//

#import "BTProfileViewController.h"
#import "BTEditVCardViewController.h"
#import "XMPPvCardTemp.h"

@interface BTProfileViewController ()<BTEditVCardViewControllerDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
/**
 *  用户头像
 */
@property (weak, nonatomic) IBOutlet UIImageView *avatarImgView;
/**
 *  昵称
 */
@property (weak, nonatomic) IBOutlet UILabel *nickName;
/**
 *  微信号
 */
@property (weak, nonatomic) IBOutlet UILabel *wechatNumLabel;
/**
 *  公司
 */
@property (weak, nonatomic) IBOutlet UILabel *orgNameLabel;
/**
 *  部门
 */
@property (weak, nonatomic) IBOutlet UILabel *departmentLabel;
/**
 *  职位
 */
@property (weak, nonatomic) IBOutlet UILabel *proLable;
/**
 *  电话
 */
@property (weak, nonatomic) IBOutlet UILabel *telLable;
/**
 *  邮箱
 */
@property (weak, nonatomic) IBOutlet UILabel *emailLabel;

@property (strong, nonatomic) UIImagePickerController *imgPickerVC;

@end

@implementation BTProfileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.获取电子名片
    XMPPvCardTemp *vCardTemp = [BTXMPPTool sharedBTXMPPTool].vCard.myvCardTemp;
    if (vCardTemp.photo) {
    self.avatarImgView.image = [UIImage imageWithData:vCardTemp.photo];
    }
    self.nickName.text = vCardTemp.nickname;
    self.wechatNumLabel.text = [BTAccout shareAccount].loginUser;
    self.orgNameLabel.text = vCardTemp.orgName;
    // 部门
    if (vCardTemp.orgUnits.count > 0) {
        self.departmentLabel.text = vCardTemp.orgUnits[0];

    }
    
    // 职位
    self.proLable.text = vCardTemp.title;
    /**
        self.telLable.text = vCardTemp.telecomsAddresses[0];
     *  - (NSArray *)telecomsAddresses { return nil; }
        使用note充当电话
     */
    self.telLable.text = vCardTemp.note;

    // 使用mailer充当邮箱
//    self.emailLabel.text = vCardTemp.mailer;
//    self.emailLabel.text = vCardTemp.emailAddresses;
    NSArray *emails = vCardTemp.emailAddresses;
    if (emails.count > 0) {
        self.emailLabel.text = emails[0];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /**
     *  根据cell不同的Tag进行相应的操作
        tag = 0 换头像
        tag = 1 调到下一个控制器
        tag = 2 不做任何操作
     */
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    switch (selectedCell.tag) {
        case 0:
            BTLog(@"换头像");
            [self chooseImg];
            break;
        case 1:
            BTLog(@"进入下一个控制器");
            [self performSegueWithIdentifier:@"toEditVcSegue" sender:selectedCell];
            break;
        case 2:
            BTLog(@"不做任何操作");
            break;
        default:
            break;
    }
}
#pragma mark -- 更改头像
-(void)chooseImg
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"请选择" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *takePictureBtn = [UIAlertAction actionWithTitle:@"照相" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        [self chooseImgFromSourceType:sourceType];
       
            }];
    
    UIAlertAction *photoLibBtn = [UIAlertAction actionWithTitle:@"图片库" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self chooseImgFromSourceType:sourceType];
            }];
    
    UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertVc addAction:takePictureBtn];
    [alertVc addAction:photoLibBtn];
    [alertVc addAction:cancelBtn];
    [self presentViewController:alertVc animated:YES completion:nil];
}
#pragma mark -- 选取图片
-(void)chooseImgFromSourceType:(UIImagePickerControllerSourceType)sourceType
{
     UIImagePickerController *imgPickerVC = [[UIImagePickerController alloc]init];
     if ([UIImagePickerController isSourceTypeAvailable:sourceType]) {
        imgPickerVC.sourceType = sourceType;
     }
    imgPickerVC.delegate = self;
    imgPickerVC.allowsEditing = YES;
    [self presentViewController:imgPickerVC animated:YES completion:nil];
    _imgPickerVC = imgPickerVC;
}
#pragma mark 图片选择器的代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    BTLog(@"%@",info);
    
    // 获取修改后的图片
    UIImage *editImg = info[UIImagePickerControllerEditedImage];
    
    // 更改cell里的图片
    self.avatarImgView.image = editImg;
    
    // 移除图片选择控制器
    [self dismissViewControllerAnimated:YES completion:nil];
    
    // 把新的图片保存到服务器
    [self editVCardViewController:nil didFinishSave:nil];
        
  
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    BTLog(@"头像更改取消");
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -- 当从当前控制器跳转到下一个控制器时系统会自动调用该方法
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    id desVc = segue.destinationViewController;
    if ([desVc isKindOfClass:[BTEditVCardViewController class]]) {
        BTEditVCardViewController *editCardVc = desVc;
        editCardVc.cell = sender;
        
        // 设置代理
        editCardVc.delegate = self;
    }
}
#pragma mark -- 编辑电子名片的代理
-(void)editVCardViewController:(BTEditVCardViewController *)editVCardVc didFinishSave:(id)sender
{
    // 1.获取电子名片
   XMPPvCardTemp *myvCard = [BTXMPPTool sharedBTXMPPTool].vCard.myvCardTemp;
    
    myvCard.photo = UIImageJPEGRepresentation(self.avatarImgView.image, 1.0);
    
    // 2.重新设置电子名片的属性值
    myvCard.nickname = self.nickName.text;
    myvCard.orgName = self.orgNameLabel.text;
    myvCard.title = self.proLable.text;
    
    if (self.departmentLabel.text != nil) {
        myvCard.orgUnits = @[self.departmentLabel.text];
    }
    
    myvCard.note = self.telLable.text;
//    myvCard.mailer = self.emailLabel.text;
//    myvCard.emailAddresses = self.emailLabel.text;
    if (self.emailLabel.text != nil) {
        myvCard.emailAddresses = @[self.emailLabel.text];
    }
    // 3.将数据上传到服务器，更新电子名片
    [[BTXMPPTool sharedBTXMPPTool].vCard updateMyvCardTemp:myvCard];
}

@end
